//
//  MBTabbarController.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/21.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBTabbarController.h"
#import "MBNavigationController.h"
#import "MBHomeViewController.h"
#import "MBGoodsViewController.h"
#import "MBOrderViewController.h"
#import "MBStatisticsViewController.h"
#import "AppDelegate.h"
#import "MBGoodsViewModel.h"
#import "MBStatisticsViewModel.h"
#import "MBOrderViewModel.h"
#import "MBHomeViewModel.h"


@interface MBTabbarController ()<UITabBarControllerDelegate>
@property (nonatomic,assign)int lastTag;
@end

@implementation MBTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChildViewController];

}


-(void)setupChildViewController
{
    self.delegate = self;
    UITabBarItem* item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont lf_systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithR:227 G:174 B:70]} forState:UIControlStateSelected];
    [item setTitleTextAttributes:@{NSFontAttributeName:[UIFont lf_systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithR:152 G:152 B:152]} forState:UIControlStateNormal];

    MBNavigationController* navi1 = [self setUpChildViewControllerWithVC:[[MBHomeViewController alloc]initWithViewModel:[[MBHomeViewModel alloc]init]] andSelectImage:[UIImage imageNamed:@"main_1_on"] andNormalImage:[UIImage imageNamed:@"main_1"] andTitle:@"首页" andTag:0];

    MBGoodsViewModel* goodsViewModel = [[MBGoodsViewModel alloc]init];
    MBNavigationController* navi2 = [self setUpChildViewControllerWithVC:[[MBGoodsViewController alloc]initWithViewModel:goodsViewModel] andSelectImage:[UIImage imageNamed:@"main_2_on"] andNormalImage:[UIImage imageNamed:@"main_2"] andTitle:@"商品管理" andTag:1];
    
    MBNavigationController* navi3 = [self setUpChildViewControllerWithVC:[[MBOrderViewController alloc]initWithViewModel:[[MBOrderViewModel alloc]init]] andSelectImage:[UIImage imageNamed:@"main_3_on"] andNormalImage:[UIImage imageNamed:@"main_3"] andTitle:@"订单管理" andTag:2];

    MBNavigationController* navi4 = [self setUpChildViewControllerWithVC:[[MBStatisticsViewController alloc]initWithViewModel:[[MBStatisticsViewModel alloc]init]] andSelectImage:[UIImage imageNamed:@"main_4_on"] andNormalImage:[UIImage imageNamed:@"main_4"] andTitle:@"统计结算" andTag:3];


    self.viewControllers = @[navi1,navi2,navi3,navi4];
    
    self.selectedIndex = 0;
    self.nowClickNavc = self.childViewControllers[0];
}



-(MBNavigationController*)setUpChildViewControllerWithVC:(MBBaseViewController*)vc andSelectImage:(UIImage*)selectImage andNormalImage:(UIImage*)normalImage andTitle:(NSString*)title andTag:(int)tag
{
    MBNavigationController* navi = [[MBNavigationController alloc]initWithRootViewController:vc];
    UITabBarItem* item = [[UITabBarItem alloc]initWithTitle:title image:[normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    navi.tabBarItem = item;
    item.tag = tag;
    
    return navi;
}

-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    MBNavigationController* nav = (MBNavigationController*)viewController;
//    NSInteger tag = nav.tabBarItem.tag;
//    if (tag == 1 || tag == 3){
//        MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
//        if (!userModel.token || [userModel.token isEqualToString:@""]) {
//            [self.nowClickNavc pushLoginVCIndex:tag];
//            return NO;
//        }else if (nav.viewControllers.count == 2){
//            nav.viewControllers = @[nav.viewControllers[0]];
//        }
//    }
//    if (tag != 1) {
//        [self.tabBar setHidden:NO];
//    }
    self.nowClickNavc = nav;
    return YES;
}

-(void)setClickIndex:(int)clickIndex
{
    _clickIndex = clickIndex;
    self.selectedIndex = clickIndex;
    self.nowClickNavc = self.childViewControllers[clickIndex];

//    if (clickIndex != 1) {
//        [self.tabBar setHidden:NO];
//    }
}

@end
